package com.rongke.web.quartz;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.model.*;
import com.rongke.service.*;
import com.rongke.sms.SmsService;
import com.rongke.sms.SmsTempletEnum;
import com.rongke.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component("FenqiJob")
@Transactional
@CrossOrigin
public class FenqiJob {
    @Autowired
    private UserService userService;
    @Autowired
    private LoanOrderService loanOrderService;
    @Autowired
    private ParamSettingService paramSettingService;
    @Autowired
    private MsgModelService msgModelService;
    @Autowired
    private SmsService smsService;
    @Autowired
    private FenqiPlanService fenqiPlanService;

    public void printMessage() throws ParseException {
        //找到所有要处理的分期计划
        EntityWrapper<FenqiPlan> ewOrder = new EntityWrapper();
        List<Integer> list = new ArrayList<Integer>() {{
            add(3);
            add(4);
            add(5);
        }};
        ewOrder.in("fenqi_order_status", list);
        List<FenqiPlan> fenqiPlanList = fenqiPlanService.selectList(ewOrder);
        if (fenqiPlanList.size()!=0){
            for (FenqiPlan f:fenqiPlanList){
                //利率
                EntityWrapper<ParamSetting> ewParam= new EntityWrapper();
                ewParam.eq("id",f.getParamSettingId());
                ewParam.eq("status",1);
                ParamSetting paramSetting = paramSettingService.selectOne(ewParam);

                EntityWrapper ew = new EntityWrapper();
                ew.eq("id",f.getOrderId());
                LoanOrder loanOrder = loanOrderService.selectOne(ew);
                if(f.getFenqiOrderStatus()==3) {
                    //判断是否到容限期（即逾期）
                    if (f.getLimitPayTime().getTime() < DateUtils.YYMMDDDate(new Date()).getTime()) {
                        f.setFenqiOrderStatus(4);
                        loanOrder.setOrderStatus(4);
                        //容限期（即逾期）一天费用
                        BigDecimal money = f.getBorrowMoney().multiply(new BigDecimal(paramSetting.getAllowPercent()*0.01)).setScale(1, BigDecimal.ROUND_HALF_UP);
                        f.setAllowMoney(money);
                        f.setAllowDays(1);
                        f.setOverdueDays(1);
                        f.setOverdueMoney(money);
                        f.setNeedPayMoney(f.getNeedPayMoney().add(money));
                        loanOrder.setNeedPayMoney(loanOrder.getNeedPayMoney().add(money));
                        fenqiPlanService.updateById(f);
                        loanOrderService.updateById(loanOrder);
                        //发送逾期短信
                        EntityWrapper<MsgModel> ewMsg = new EntityWrapper();
                        ewMsg.eq("type",5);
                        MsgModel msgModel = msgModelService.selectOne(ewMsg);//找到短信模板
                        //将信息部分替换
                        User user = userService.selectById(f.getUserId());
                        String content = msgModel.getContent().replaceAll("</?[^>]+>", "")
                                .replace("#userName",user.getUserName())
                                .replace("#borrowMoney",f.getBorrowMoney().toString())
                                .replace("overDays",f.getOverdueDays().toString())
                                .replace("#allowMoney",f.getAllowMoney().toString())
                                .replace("#needPayMoney",f.getNeedPayMoney().setScale(2, RoundingMode.HALF_UP).toString());
                        SmsTempletEnum.REMINDSEND.setTypeName(content);
                        smsService.smsCodeByType(user.getPhone(), 3);
                    }
                    //判断是否为还款前一天
                    else if(f.getLimitPayTime().getTime() == DateUtils.dayAdd(1,DateUtils.YYMMDDDate(new Date())).getTime()){
                        //发送短信
                        EntityWrapper<MsgModel> ewMsg = new EntityWrapper();
                        ewMsg.eq("type",2);
                        MsgModel msgModel = msgModelService.selectOne(ewMsg);//找到短信模板
                        //将信息部分替换
                        User user = userService.selectById(f.getUserId());
                        String content = msgModel.getContent().replaceAll("</?[^>]+>", "")
                                .replace("#userName",user.getUserName())
                                .replace("#money",f.getNeedPayMoney().setScale(1, BigDecimal.ROUND_HALF_UP).toString());
                        SmsTempletEnum.REMINDSEND.setTypeName(content);
                        smsService.smsCodeByType(user.getPhone(), 3);
                    }else if(f.getLimitPayTime().getTime() == DateUtils.YYMMDDDate(new Date()).getTime()){
                        EntityWrapper<MsgModel> ewMsg = new EntityWrapper();
                        ewMsg.eq("type",3);
                        MsgModel msgModel = msgModelService.selectOne(ewMsg);//找到短信模板
                        //将信息部分替换
                        User user = userService.selectById(f.getUserId());
                        String content = msgModel.getContent().replaceAll("</?[^>]+>", "")
                                .replace("userName",user.getUserName())
                                .replace("#money",f.getNeedPayMoney().toString());
                        SmsTempletEnum.REMINDSEND.setTypeName(content);
                        smsService.smsCodeByType(user.getPhone(), 3);
                    }
                }
                //状态为容限期的时候
                else if(f.getFenqiOrderStatus()==4){
                    //判断是否超出容限期
                    if(f.getOverdueTime().getTime()< DateUtils.YYMMDDDate(new Date()).getTime()){
                        f.setFenqiOrderStatus(5);
                        loanOrder.setOrderStatus(5);
                        //超出容限期一天的费用
                        BigDecimal money = f.getBorrowMoney().multiply(new BigDecimal(paramSetting.getOverduePercent()*0.01)).setScale(1, BigDecimal.ROUND_HALF_UP);
                        f.setOverdueDays(f.getOverdueDays()+1);
                        f.setOverdueMoney(f.getOverdueMoney().add(money));
                        f.setNeedPayMoney(f.getNeedPayMoney().add(money));
                        loanOrder.setNeedPayMoney(loanOrder.getNeedPayMoney().add(money));
                        fenqiPlanService.updateById(f);
                        loanOrderService.updateById(loanOrder);
                        //发送逾期短信
                        EntityWrapper<MsgModel> ewMsg = new EntityWrapper();
                        ewMsg.eq("type",5);
                        MsgModel msgModel = msgModelService.selectOne(ewMsg);//找到短信模板
                        //将信息部分替换
                        User user = userService.selectById(f.getUserId());
                        String content = msgModel.getContent().replaceAll("</?[^>]+>", "")
                                .replace("#userName",user.getUserName())
                                .replace("#borrowMoney",f.getBorrowMoney().toString())
                                .replace("overDays",f.getOverdueDays().toString())
                                .replace("#allowMoney",f.getAllowMoney().toString())
                                .replace("#needPayMoney",f.getNeedPayMoney().toString());
                        SmsTempletEnum.REMINDSEND.setTypeName(content);
                        smsService.smsCodeByType(user.getPhone(), 3);
                    }else{
                        //容限期一天的费用
                        BigDecimal money = f.getBorrowMoney().multiply(new BigDecimal(paramSetting.getAllowPercent()*0.01)).setScale(1, BigDecimal.ROUND_HALF_UP);
                        f.setAllowMoney(f.getAllowMoney().add(money));
                        f.setAllowDays(f.getAllowDays()+1);
                        f.setOverdueDays(f.getOverdueDays()+1);
                        f.setOverdueMoney(f.getOverdueMoney().add(money));
                        f.setNeedPayMoney(f.getNeedPayMoney().add(money));
                        loanOrder.setNeedPayMoney(loanOrder.getNeedPayMoney().add(money));
                        fenqiPlanService.updateById(f);
                        loanOrderService.updateById(loanOrder);
                        //发送逾期短信
                        EntityWrapper<MsgModel> ewMsg = new EntityWrapper();
                        ewMsg.eq("type",5);
                        MsgModel msgModel = msgModelService.selectOne(ewMsg);//找到短信模板
                        //将信息部分替换
                        User user = userService.selectById(f.getUserId());
                        String content = msgModel.getContent().replaceAll("</?[^>]+>", "")
                                .replace("#userName",user.getUserName())
                                .replace("#borrowMoney",f.getBorrowMoney().toString())
                                .replace("overDays",f.getOverdueDays().toString())
                                .replace("#allowMoney",f.getAllowMoney().toString())
                                .replace("#needPayMoney",f.getNeedPayMoney().setScale(2,RoundingMode.HALF_UP).toString());
                        SmsTempletEnum.REMINDSEND.setTypeName(content);
                        smsService.smsCodeByType(user.getPhone(), 3);
                    }
                }
                //状态超出容限期时
                else if(f.getFenqiOrderStatus()==5){
                    //超出容限期一天的费用
                    if(f.getOverdueDays()<=60){
                        BigDecimal money = f.getBorrowMoney().multiply(new BigDecimal(paramSetting.getOverduePercent()*0.01)).setScale(1, BigDecimal.ROUND_HALF_UP);
                        f.setOverdueMoney(f.getOverdueMoney().add(money));
                        f.setNeedPayMoney(f.getNeedPayMoney().add(money));
                        loanOrder.setNeedPayMoney(loanOrder.getNeedPayMoney().add(money));
                    }
                    f.setOverdueDays(f.getOverdueDays()+1);
                    fenqiPlanService.updateById(f);
                    loanOrderService.updateById(loanOrder);
                    //发送逾期短信
                    EntityWrapper<MsgModel> ewMsg = new EntityWrapper();
                    ewMsg.eq("type",5);
                    MsgModel msgModel = msgModelService.selectOne(ewMsg);//找到短信模板
                    //将信息部分替换
                    User user = userService.selectById(f.getUserId());
                    String content = msgModel.getContent().replaceAll("</?[^>]+>", "")
                            .replace("#userName",user.getUserName())
                            .replace("#borrowMoney",f.getBorrowMoney().toString())
                            .replace("overDays",f.getOverdueDays().toString())
                            .replace("#allowMoney",f.getAllowMoney().toString())
                            .replace("#needPayMoney",f.getNeedPayMoney().setScale(2,RoundingMode.HALF_UP).toString());
                    SmsTempletEnum.REMINDSEND.setTypeName(content);
                    smsService.smsCodeByType(user.getPhone(), 3);
                }
            }
        }

    }
}
